﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Common.Reflection.DAL.Managers
{
    internal class DBHelper
    {

        internal static void Operator(PropertyInfo p, string ptype, object obj, object t, int theOperator)
        {
        //    Common.LogManager.GetInstance().Debug(t.ToString() + "---" + ptype);
            if (obj != null)
            {
                if (obj.ToString() != "")
                {

                    if (ptype.Equals("System.String"))
                    {
                        if (obj != null)
                        {
                            if (obj.ToString().ToLower().IndexOf("<script") < 0)
                            {
                                p.SetValue(t, obj, null);
                            }
                            else
                            {
                                //   Common.LogManager.GetInstance().Log("所查询的数据含有恶意标签<script>");
                            }
                        }
                        else
                        {
                            p.SetValue(t, obj, null);
                        }
                    }
                    else if (ptype.Equals("System.Int32"))
                    {
                        if (Entity.IsDivideByHundred(p))
                        {
                            if (theOperator == 1)
                            {
                                p.SetValue(t, Convert.ToInt32(obj) / 100, null);
                            }
                            else
                            {
                                p.SetValue(t, Convert.ToInt32(obj) * 100, null);
                            }
                        }
                        else
                        {
                            p.SetValue(t, Convert.ToInt32(obj), null);
                        }
                    }
                    else if (ptype.Equals("System.Double"))
                    {
                        if (Entity.IsDivideByHundred(p))
                        {
                            if (theOperator == 1)
                            {
                                p.SetValue(t, Convert.ToDouble(obj) / 100, null);
                            }
                            else
                            {
                                p.SetValue(t, Convert.ToDouble(obj) * 100, null);
                            }
                        }
                        else if (Entity.IsMoney(p))
                        {
                            p.SetValue(t, Convert.ToDouble(obj.ToString().Replace(",", "")), null);
                        }
                        else
                        {
                            p.SetValue(t, Convert.ToDouble(obj), null);
                        }
                    }
                    else if (ptype.Equals("System.Boolean"))
                    {
                        p.SetValue(t, Convert.ToBoolean(obj), null);
                    }
                    else if (ptype.Equals("System.DateTime"))
                    {
                        p.SetValue(t, Convert.ToDateTime(obj), null);
                    }
                    else if (ptype.Equals("System.Nullable`1[System.Int32]"))
                    {
                        if (Entity.IsDivideByHundred(p))
                        {
                            if (theOperator == 1)
                            {
                                p.SetValue(t, Convert.ToInt32(obj) / 100, null);
                            }
                            else
                            {
                                p.SetValue(t, Convert.ToInt32(obj) * 100, null);
                            }
                        }
                        else
                        {
                            p.SetValue(t, Convert.ToInt32(obj), null);
                        }
                    }
                    else if (ptype.Equals("System.Nullable`1[System.Boolean]"))
                    {
                        p.SetValue(t, Convert.ToBoolean(obj), null);
                    }
                    else if (ptype.Equals("System.Decimal"))
                    {
                        if (Entity.IsDivideByHundred(p))
                        {
                            if (theOperator == 1)
                            {
                                p.SetValue(t, Convert.ToDecimal(obj) / 100, null);
                            }
                            else
                            {
                                p.SetValue(t, Convert.ToDecimal(obj) * 100, null);
                            }
                        }
                        else if (Entity.IsMoney(p))
                        {
                            p.SetValue(t, Convert.ToDecimal(obj.ToString().Replace(",", "")), null);
                        }
                        else
                        {
                            p.SetValue(t, Convert.ToDecimal(obj), null);
                        }
                    }
                    else if (ptype.Equals("System.Nullable`1[System.Decimal]"))
                    {
                        if (Entity.IsDivideByHundred(p))
                        {
                            if (theOperator == 1)
                            {
                                p.SetValue(t, Convert.ToDecimal(obj) / 100, null);
                            }
                            else
                            {
                                p.SetValue(t, Convert.ToDecimal(obj) * 100, null);
                            }
                        }
                        else if (Entity.IsMoney(p))
                        {
                            p.SetValue(t, Convert.ToDecimal(obj.ToString().Replace(",", "")), null);
                        }
                        else
                        {
                            p.SetValue(t, Convert.ToDecimal(obj), null);
                        }
                    }

                }
                else
                {
                    p.SetValue(t, null, null);
                }
            }
            else
            {
                p.SetValue(t, null, null);
            }
        }

        internal static int GetTableCountByKeys(string tableName, string sqlparams, string DBType, string key)
        {
            int cols = 0;
            string sql = string.Format("select count(" + key + ") from {0} where {1}", tableName, sqlparams);
            if (DBType.ToString().Equals("sql"))
            {
                cols = Convert.ToInt32(ADO.Tools.DbHelperSQL.GetSingle(sql));
            }
            else if (DBType.ToString().Equals("oracle"))
            {
                cols = Convert.ToInt32(ADO.Tools.OracleHelper.GetSingle(sql));
            }
            return cols;
        }


        /// <summary>
        /// 得到表的记录总数
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        internal static int GetTableCount(string tableName, string DBType, string key)
        {
            int cols = 0;
            string sql = string.Format("select count(" + key + ") from {0}", tableName);
            if (DBType.ToString().Equals("sql"))
            {
                cols = Convert.ToInt32(ADO.Tools.DbHelperSQL.GetSingle(sql));
            }
            else if (DBType.ToString().Equals("oracle"))
            {
                cols = Convert.ToInt32(ADO.Tools.OracleHelper.GetSingle(sql));
            }
            else if (DBType.ToString().Equals("access"))
            {
                cols = Convert.ToInt32(ADO.Tools.AccessHelper.ExecuteScalar(sql));
            }
            return cols;
        }
    }
}
